package com.xbfinal.myzhxy.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbfinal.myzhxy.mapper.AdminMapper;
import com.xbfinal.myzhxy.pojo.Admin;
import com.xbfinal.myzhxy.pojo.LoginForm;
import com.xbfinal.myzhxy.service.AdminService;
import com.xbfinal.myzhxy.util.MD5;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("adminServiceImpl")//可以通过adminServiceImpl获取AdminServiceImpl对象
@Transactional//事务管理
public class AdminServiceImpl
        extends ServiceImpl<AdminMapper,Admin>
        implements AdminService {
    @Override
   public Admin login(LoginForm loginForm){
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name", loginForm.getUsername());
        //MD5.encrypt()把密码加密再对比
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));
        final Admin admin = baseMapper.selectOne(queryWrapper);
        return admin;
    }

    @Override
    public Admin getAdminById(Long userId) {
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        return  baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<Admin> getAdmins(Page<Admin> pageParam, String adminName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(adminName)){
            queryWrapper.like("name",adminName);
        }
        queryWrapper.orderByDesc("id");
        queryWrapper.orderByAsc("name");
        Page page = baseMapper.selectPage(pageParam, queryWrapper);
        return page;
    }
}
